﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Xml.Serialization;

namespace WebApplication.AppCode
{
    public class Tools
    {
        /// <summary>
        /// 执行DES加密
        /// </summary>
        public static string Encrypt(string mge)
        {

            string str = "";
            if (!string.IsNullOrEmpty(mge))
            {
                try
                {
                    byte[] MyStr_E = Encoding.UTF8.GetBytes(mge);
                    byte[] MyKey_E = Encoding.UTF8.GetBytes("*&^%$#@!");

                    DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
                    MyDes_E.Key = MyKey_E;
                    MyDes_E.IV = MyKey_E;

                    MemoryStream MyMem_E = new MemoryStream();

                    CryptoStream MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
                    MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
                    MyCry_E.Flush();
                    MyCry_E.Close();


                    str = Convert.ToBase64String(MyMem_E.ToArray());
                    MyMem_E.Close();
                }
                catch (Exception)
                {
                }
            }

            return str;
        }

        /// <summary>
        /// 执行DES解密
        /// </summary>
        public static string Decrypt(string mge)
        {

            string str = "";
            if (!string.IsNullOrEmpty(mge))
            {
                try
                {
                    byte[] MyStr_D = Convert.FromBase64String(mge);
                    byte[] MyKey_D = Encoding.UTF8.GetBytes("*&^%$#@!");

                    DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
                    MyDes_D.Key = MyKey_D;
                    MyDes_D.IV = MyKey_D;

                    MemoryStream MyMem_D = new MemoryStream();

                    CryptoStream MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);
                    MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
                    MyCry_D.Flush();
                    MyCry_D.Close();



                    str = Encoding.UTF8.GetString(MyMem_D.ToArray());

                    MyMem_D.Close();
                }
                catch (Exception)
                {
                }
            }

            return str;
        }

        public static string Serialize<T>(T item)
        {
            StringWriter writer=new StringWriter();
            XmlSerializer serializer = new XmlSerializer(item.GetType());
            serializer.Serialize(writer, item);
            return writer.ToString();
        }

        public static T Deserialize<T>(string xml)
        {
            T item=default(T);
            StringReader writer = new StringReader(xml);
            XmlSerializer serializer = new XmlSerializer(item.GetType());
            return (T)serializer.Deserialize(writer);
        }
    }
}